<?php namespace sacvoyage\http;

use \HttpRequest;

class Browser {
	
	public function __construct() {
		$this->reset();
	}
	
	public function reset() {
		if($this->_curl) {
			curl_close($this->_curl);
		}
		$this->_curl=curl_init();
		curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->_curl, CURLOPT_HEADER, 1);
	}
	
	public function request($method, $url, $body=null, $headers=array()) {
		if(!$headers) {
			$headers=array();
		}
		if($this->_cookie) {
			curl_setopt($this->_curl, CURLOPT_COOKIE, $this->_cookie);
		}
		
		curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($this->_curl, CURLOPT_URL, $url);
		curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
		curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
		
		$data=curl_exec($this->_curl);
		$headerSize=curl_getinfo($this->_curl, CURLINFO_HEADER_SIZE);
		$headerStr=substr($data, 0, $headerSize);
		$body=substr($data, $headerSize);
		
		// parse headers
		$headers=array();
		$lines=explode("\r\n", $headerStr);
		foreach($lines as $line) {
			if($line) {
				$parts=explode(":", $line, 2);
				if(count($parts)==2) {
					$headers[trim($parts[0])]=trim($parts[1]);
				}
			}
		}
		
		if(isset($headers['Set-Cookie'])) {
			$this->_cookie=$headers['Set-Cookie'];
		}
		
		return array(
			'status'=>curl_getinfo($this->_curl, CURLINFO_HTTP_CODE),
			'headers'=>$headers,
			'body'=>$body		
		);		
	}
	
	private $_cookie;
	private $_curl;
	/*
	// -----------------
	
	public function get($url, $params=Null, $headers=Null) {
		if($params) {
			reset($params);
			$url.='?'.urlencode(key($params)).'='.urlencode(current($params));
			next($params);
			while(current($params)) {
				$url.='&'.urlencode(key($params)).'='.urlencode(current($params));	
				next($params);
			}
		}
		curl_setopt($this->_curl, CURLOPT_URL, $url);
		curl_setopt($this->_curl, CURLOPT_HTTPGET, 1);
		
		if($headers) {
			curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
		}
		return curl_exec($this->_curl);
	}
	
	public function post($url, $params='', $headers=Null) {
		curl_setopt($this->_curl, CURLOPT_URL, $url);
		curl_setopt($this->_curl, CURLOPT_POST, 1);
		
		if(!$headers) {
			$headers=array();
		}
		
		if(is_array($params)) {
			$headers['Content-Type']='application/x-www-form-urlencoded';
			$params=http_build_query($params);			
		}
		
		if($params) {
			curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $params);
		}

		curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
		return curl_exec($this->_curl);
	}
	
	public function delete($url, $params=Null, $headers=Null) {
		if($params) {
			reset($params);
			$url.='?'.urlencode(key($params)).'='.urlencode(current($params));
			next($params);
			while(current($params)) {
				$url.='&'.urlencode(key($params)).'='.urlencode(current($params));	
				next($params);
			}
		}
		curl_setopt($this->_curl, CURLOPT_URL, $url);
		curl_setopt($this->_curl, CURLOPT_HTTPDELETE, 1);
		
		if($headers) {
			curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
		}
		return curl_exec($this->_curl);
	}
	
	
	
	public function getHeader($name) {
				
	}
	
	public function getHTTPCode() {
		return curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);		
	}
	
	
	
	
	
	
	*/
}

?>
